<template>
  <el-dialog title="新增员工" :visible="showDialog" @close="closeDialog">
    <el-form
      ref="formRef"
      label-width="100px"
      :model="formData"
      :rules="rules"
    >
      <el-form-item label="员工姓名" prop="username">
        <el-input v-model="formData.username" style="width: 80%" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="formData.mobile" style="width: 80%" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="formData.timeOfEntry" style="width: 80%" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="formData.formOfEmployment" style="width: 80%">
          <el-option v-for="item in hireType" :key="item.id" :value="item.id" :label="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="formData.workNumber" style="width: 80%" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-cascader
          v-model="formData.departmentName"
          style="width: 80%;"
          :options="options"
          :props="{
            label: 'name', // 展示的内容对应的数据对象的key
            checkStrictly: true, // 是否严格遵循父子节点不联动
            value: 'name', // 节点选中的值
            emitPath: false // 选中的时候，是否返回当前节点对应所有父节点的值组成数组，如果false只返回当前节点的值(string类型)
          }"
          @focus="getDepartments"
        />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker
          v-model="formData.correctionTime"
          style="width: 80%"
        />
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-col :span="12">
          <el-button size="small" @click="closeDialog">取消</el-button>
          <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        </el-col>
      </el-row>
    </template>
  </el-dialog>
</template>
<script>
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import EmployeeEnum from '@/api/constant/employees'
import { addEmployee } from '@/api/employees'

export default {
  name: 'AddEmp',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      options: [],
      hireType: EmployeeEnum.hireType,
      formData: {
        username: '', // 用户姓名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门名称
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      rules: {
        username: [{ required: true, message: '用户姓名不能为空', trigger: 'blur' }, {
          min: 1, max: 4, message: '用户姓名为1-4位'
        }],
        mobile: [{ required: true, message: '手机号不能为空', trigger: 'blur' }, {
          pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur'
        }],
        formOfEmployment: [{ required: true, message: '聘用形式不能为空', trigger: 'blur' }],
        workNumber: [{ required: true, message: '工号不能为空', trigger: 'blur' }],
        departmentName: [{ required: true, message: '部门不能为空', trigger: 'change' }],
        timeOfEntry: [{ required: true, message: '入职时间', trigger: 'blur' }]
      }
    }
  },
  methods: {
    async btnOk() {
      //  提交表单数据
      //  1、校验表单、再进行提交
      await this.$refs.formRef.validate()
      //  2、调用接口
      await addEmployee(this.formData)
      //  3、提示
      this.$message.success('新增员工成功')
      //  4、重新拉去列表数据（获取列表的方法不在子组件）
      //   通过parent调用父组件的重新获取列表的方法
      this.$parent.handleGetList()
      //  5、弹层关掉(重置表单 数据/校验)
      this.closeDialog()
    },
    async getDepartments() {
      const { depts } = await getDepartments()
      this.options = tranListToTreeData(depts, '')
    },
    closeDialog() {
      //  修改父组件的弹层标识，变为false
      this.formData = {
        username: '', // 用户姓名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门名称
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      }
      this.$refs.formRef.resetFields()
      this.$emit('update:showDialog', false)
    }
  }
}
</script>
